//timer.c -- initialises the PIT, and handles clock updates

#include <timer.h>
#include <isr.h>
#include <monitor.h>
#include <task.h>
#include <drivers/mouse.h>

u32int tick = 0;

extern volatile task_t *current_task;
extern volatile task_t *ready_queue;
extern page_directory_t *current_directory;

extern u32int read_eip();

static void timer_callback(registers_t regs)
{
	tick++;
	switch_task();
	
}

void init_timer(u32int frequency)
{
	//Firstly, regiser our timer callback
	register_interrupt_handler(IRQ0, &timer_callback);

	//The value we send to the PIT is the value to devide it's input clock
	// (1193180 HZ) by, to get our required frequency. Important to note is
	//that the divisor must be small enough to fit into 16 bits
	u32int divisor = 1193180 / frequency;
	
	//send the command byte
	outb (0x43, 0x36);
	
	//Divisor has to be send byte-wise, so split here into upper/lower bytes
	u8int l = (u8int) (divisor & 0xFF);
	u8int h = (u8int) ( (divisor >> 8) & 0xFF);

	//send the frequency divisor 
	outb(0x40, l);
	outb(0x40, h);
}
	
